package com.tzbank.common.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.tzbank.common.consumer.TzBankWisdomScheduler;
import com.tzbank.common.dao.CommonMapper;
import com.tzbank.common.dao.ThemeMapper;
import com.tzbank.common.entity.dto.FlowDTO;
import com.tzbank.common.entity.dto.PageRuleDTO;
import com.tzbank.common.service.CommonService;
import com.tzbank.common.service.ProcessOptimizationService;
import com.tzbank.common.service.ThemeService;
import com.tzbank.entity.Order;
import com.tzbank.entity.StandardPageset;
import com.tzbank.entity.StandardRuleset;
import com.tzbank.entity.ThemeDict;
import com.tzbank.redis.RedisUtils;
import com.tzbank.utils.CodeUtil;
import com.tzbank.utils.ResultUtil;

import net.sf.json.JSONObject;


@Service(value = "themeService")
public class ThemeServiceImpl implements ThemeService {

	@Autowired
    private ThemeMapper themeMapper;
	
	@Autowired
    private CommonMapper commonMapper;
	
	@Autowired
	private CommonService commonService;
	
	@Autowired
	TzBankWisdomScheduler tzBankWisdomScheduler;
	
	@Autowired
	private ProcessOptimizationService processOptimizationService;
	
	/**
	 * 获取主题页面流程
	 */
	public String getThemePageFlow(Map<String,String> reqBody){
		System.out.println("===============>"+reqBody.toString());
		String themeId = reqBody.get("themeId");			//业务主题ID
		
    	//主题ID判断
    	if(StringUtils.isBlank(themeId)) {
    		 return JSONObject.fromObject(ResultUtil.resultMap(CodeUtil.CODE_10000.getName(), CodeUtil.CODE_10000.getCode(), reqBody)).toString()  ;
    	}
		
		//获取标准集
		Map<String, Object> map = getStandardSet(themeId);		
		//获取数据集
		Map<String, Object> dataset = getDataSet(reqBody);
		map.put("dataset", dataset);
		
		/**
    	 * 调用流程优化殷勤
    	 */
		Map<String,Object> paramsMap = new HashMap();
		paramsMap.put("paramsJSON", reqBody);		//参数对象
		paramsMap.put("flowJSON", map);				//流程部分
		return processOptimizationService.processOptimization(JSONObject.fromObject(paramsMap));
	}
	

	/**
	 * 获取标准集
	 */
	public Map<String, Object> getStandardSet(String themeId) {
		
		//获取工单对象
		Order order = (Order) themeMapper.selectOrderByThemeId(themeId);
		//获取流程集合
		List<FlowDTO> flowset =themeMapper.selectFlowListByThemeId(themeId);
		//获取页面集合
		List<StandardPageset> pagePartitionset =themeMapper.selectPageListByThemeId(themeId);
		
		Map rule_map = new HashMap();			//规则集	
		//获取页面规则
		List<PageRuleDTO> pageRuleset =themeMapper.selectPageRuleListByThemeId(themeId);
		rule_map.put("pagerule", pageRuleset);
		
		//获取所有的页面规则
		Set<String> rule_set = new HashSet<String>();
		for(PageRuleDTO pu : pageRuleset) {
			if(StringUtils.isNotBlank(pu.getRuleIds())) {		//判断非空
				for(String ruleId : pu.getRuleIds().split(",")) {
					rule_set.add(ruleId);
				}
			}
		}
		
		//获取页面规则
		List<String> item = new ArrayList<String>();
		item.addAll(rule_set);
		
		if(item.size() > 0) {
			List<StandardRuleset> ruleset =themeMapper.selectRuleListByRuleIds(item);
			rule_map.put("rulelist", ruleset);
		}else {
			rule_map.put("rulelist", new ArrayList());			//前端要求为空对象  
		}
		
		Map<String, Object> map =new HashMap<String, Object>();
		map.put("order", order);
		map.put("flowset", flowset);
		map.put("pageset", pagePartitionset);
		map.put("ruleset", rule_map);
		return map;
	}
	
	/**
	 * 获取数据集
	 */
	public Map<String, Object> getDataSet(Map<String,String> reqBody){
		String themeId = reqBody.get("themeId");		//主题ID
		//数据集合
    	Map<String,Object> dataset = new HashMap<String,Object>();
    	dataset.put("bean", reqBody);
    	
    	Map<String,Object> map_dict =  commonService.getListDict();		//获取字典集合
    	
    	List<ThemeDict> dictList = commonMapper.getListThemeDict(themeId);		//根据业务获取相关的字典集合
    	List<Map<String,Object>> dictResultList = new ArrayList<Map<String,Object>>();	//字典集合
    	for(ThemeDict td : dictList) {
    		Map<String,Object> dictTypeMap = new HashMap<String,Object>();
    		dictTypeMap.put("dictType", td.getDictTypeCode());		//字典类型
    		dictTypeMap.put("list", map_dict.get(td.getDictTypeCode()));		//字典类型
    		dictResultList.add(dictTypeMap);
    	}
    	dataset.put("dict", dictResultList);		//赋值主题所需的全部字典
    	
    	//用户对象
    	dataset.put("user", getUserInfo(reqBody));
		return dataset;
	}
	
	//用户数据
	private Map<String,Object> getUserInfo(Map<String,String> reqBody) {
		String clientSeqNo = reqBody.get("clientSeqNo");		//客户号
    	Map<String,Object> user = RedisUtils.getUserInfo(clientSeqNo);		//redis获取用户信息
    	if(user == null) {
    		return null;
    	}
    	user.remove("acctArray");		//移除所有账号集合
    	return user;
    }
	
}
